package com.gxc.healthfood.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxc.healthfood.entity.UserNotificationSettings;
import com.gxc.healthfood.service.UserNotificationSettingsService;
import com.gxc.healthfood.mapper.UserNotificationSettingsMapper;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author hasee
* @description 针对表【user_notification_settings】的数据库操作Service实现
* @createDate 2025-10-04
*/
@Service
public class UserNotificationSettingsServiceImpl extends ServiceImpl<UserNotificationSettingsMapper, UserNotificationSettings>
    implements UserNotificationSettingsService{

    @Override
    public UserNotificationSettings getByUserId(Long userId) {
        LambdaQueryWrapper<UserNotificationSettings> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserNotificationSettings::getUserId, userId);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean saveOrUpdateSettings(UserNotificationSettings settings) {
        // 设置更新时间
        settings.setUpdatedAt(new Date());
        
        // 如果是新增记录，设置创建时间
        if (settings.getId() == null) {
            settings.setCreatedAt(new Date());
        }
        
        return this.saveOrUpdate(settings);
    }
}